const UnionFind = require("../template")

function testUnionFindComplexScenarios() {
  console.log(
    "1. 复杂合并序列测试:",
    (() => {
      const uf = new UnionFind(10)
      
      // 复杂的合并序列
      uf.union(0, 1)
      uf.union(2, 3)
      uf.union(4, 5)
      uf.union(6, 7)
      uf.union(8, 9)
      
      // 合并不同分量
      uf.union(1, 2) // 合并 {0,1} 和 {2,3}
      uf.union(5, 6) // 合并 {4,5} 和 {6,7}
      uf.union(3, 4) // 合并 {0,1,2,3} 和 {4,5,6,7}
      
      // 验证连通性
      const connected1 = uf.connected(0, 7) // 应该连通
      const connected2 = uf.connected(8, 9) // 应该连通
      const connected3 = uf.connected(0, 8) // 应该不连通
      
      return connected1 === true &&
             connected2 === true &&
             connected3 === false
    })()
  )

  console.log(
    "2. 大量数据管理测试:",
    (() => {
      const uf = new UnionFind(5000)
      
      // 插入大量数据
      for (let i = 0; i < 5000; i += 2) {
        uf.union(i, i + 1)
      }
      
      // 随机访问测试
      const randomIndices = Array.from({ length: 100 }, () => 
        Math.floor(Math.random() * 5000)
      )
      
      const randomAccessCorrect = randomIndices.every(index => 
        uf.find(index) !== undefined
      )
      
      // 验证统计信息
      const componentCount = uf.getComponentCount()
      
      return randomAccessCorrect &&
             componentCount === 2500 // 2500个大小为2的分量
    })()
  )

  console.log(
    "3. 动态负载测试:",
    (() => {
      const uf = new UnionFind(1000)
      
      // 阶段1：创建连通分量
      for (let i = 0; i < 500; i++) {
        uf.union(i, i + 1)
      }
      
      const componentCount1 = uf.getComponentCount()
      
      // 阶段2：合并更多分量
      for (let i = 0; i < 250; i++) {
        uf.union(i * 2, i * 2 + 2)
      }
      
      const componentCount2 = uf.getComponentCount()
      
      // 阶段3：继续合并
      for (let i = 0; i < 125; i++) {
        uf.union(i * 4, i * 4 + 4)
      }
      
      const componentCount3 = uf.getComponentCount()
      
      return componentCount1 === 500 &&
             componentCount2 === 250 &&
             componentCount3 === 125
    })()
  )

  console.log(
    "4. 并发操作模拟测试:",
    (() => {
      const uf = new UnionFind(100)
      
      // 模拟多个操作同时进行
      const operations = []
      
      // 操作1：批量合并
      for (let i = 0; i < 50; i++) {
        operations.push(() => uf.union(i, i + 1))
      }
      
      // 操作2：批量查找
      for (let i = 0; i < 100; i++) {
        operations.push(() => uf.find(i))
      }
      
      // 操作3：批量连通性检查
      for (let i = 0; i < 50; i++) {
        operations.push(() => uf.connected(i, i + 1))
      }
      
      // 随机执行操作
      const shuffledOps = operations.sort(() => Math.random() - 0.5)
      shuffledOps.forEach(op => op())
      
      // 验证最终状态
      const finalComponentCount = uf.getComponentCount()
      
      return finalComponentCount === 50 // 50个大小为2的分量
    })()
  )

  console.log(
    "5. 边界条件处理测试:",
    (() => {
      const uf = new UnionFind(5)
      
      // 测试边界条件
      uf.union(0, 1)
      uf.union(2, 3)
      
      // 测试查找边界
      const find0 = uf.find(0)
      const find4 = uf.find(4)
      
      // 测试连通性边界
      const connected01 = uf.connected(0, 1)
      const connected04 = uf.connected(0, 4)
      const connected23 = uf.connected(2, 3)
      
      return find0 !== undefined &&
             find4 !== undefined &&
             connected01 === true &&
             connected04 === false &&
             connected23 === true
    })()
  )

  console.log(
    "6. 错误恢复测试:",
    (() => {
      const uf = new UnionFind(5)
      
      // 正常操作
      uf.union(0, 1)
      uf.union(2, 3)
      
      // 尝试一些边界操作
      uf.union(0, 1) // 重复合并
      uf.union(4, 4) // 自合并
      
      // 验证状态
      const componentCount = uf.getComponentCount()
      const connected01 = uf.connected(0, 1)
      const connected44 = uf.connected(4, 4)
      
      return componentCount === 3 && // {0,1}, {2,3}, {4}
             connected01 === true &&
             connected44 === true
    })()
  )

  console.log(
    "7. 内存效率测试:",
    (() => {
      const uf = new UnionFind(2000)
      
      // 添加大量数据测试内存效率
      const startMemory = process.memoryUsage ? process.memoryUsage().heapUsed : 0
      
      for (let i = 0; i < 2000; i += 2) {
        uf.union(i, i + 1)
      }
      
      const endMemory = process.memoryUsage ? process.memoryUsage().heapUsed : 0
      const memoryUsed = endMemory - startMemory
      
      // 验证统计信息
      const componentCount = uf.getComponentCount()
      
      return componentCount === 1000 &&
             memoryUsed < 100 * 1024 * 1024 // 内存使用应该合理
    })()
  )

  console.log(
    "8. 连通分量分析复杂测试:",
    (() => {
      const uf = new UnionFind(10)
      
      // 创建复杂的连通分量结构
      uf.union(0, 1)
      uf.union(1, 2)
      uf.union(3, 4)
      uf.union(5, 6)
      uf.union(6, 7)
      uf.union(8, 9)
      uf.union(2, 3) // 合并两个分量
      
      const analysis = uf.analyzeComponents()
      const components = uf.getAllComponents()
      const maxSize = uf.getMaxComponentSize()
      
      return analysis.componentCount === 3 && // {0,1,2,3,4}, {5,6,7}, {8,9}
             analysis.maxSize === 5 &&
             maxSize === 5 &&
             components.length === 3
    })()
  )

  console.log(
    "9. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        processComplex: function(size) {
          const uf = new UnionFind(size)
          for (let i = 0; i < size; i += 2) {
            uf.union(i, i + 1)
          }
          return uf.getComponentCount() * this.multiplier
        }
      }
      
      const result = complexObj.processComplex(10)
      return result === 15 // 5 * 3 = 15
    })()
  )

  console.log(
    "10. 复杂对象this绑定场景测试:",
    (() => {
      const complexConfigObj = {
        config: { 
          threshold: 3, 
          maxComponents: 10 
        },
        processWithComplexConfig: function(size) {
          const uf = new UnionFind(size)
          for (let i = 0; i < size; i += 2) {
            uf.union(i, i + 1)
          }
          
          const componentCount = uf.getComponentCount()
          
          return componentCount >= this.config.threshold &&
                 componentCount <= this.config.maxComponents
        }
      }
      
      return complexConfigObj.processWithComplexConfig(10) === true
    })()
  )

  console.log(
    "11. 极端负载测试:",
    (() => {
      const uf = new UnionFind(2000)
      
      // 在极端条件下添加大量数据
      for (let i = 0; i < 2000; i += 2) {
        uf.union(i, i + 1)
      }
      
      // 验证统计信息
      const componentCount = uf.getComponentCount()
      const maxSize = uf.getMaxComponentSize()
      
      // 测试连通分量分析
      const analysis = uf.analyzeComponents()
      const components = uf.getAllComponents()
      
      return componentCount === 1000 &&
             maxSize === 2 &&
             analysis.componentCount === 1000 &&
             components.length === 1000
    })()
  )

  console.log(
    "12. 综合复杂场景测试:",
    (() => {
      const uf = new UnionFind(100)
      
      // 综合测试：多种操作混合
      const operations = []
      
      // 插入各种数据
      for (let i = 0; i < 100; i += 2) {
        operations.push(() => uf.union(i, i + 1))
      }
      
      // 执行操作
      operations.forEach(op => op())
      
      // 合并部分分量
      for (let i = 0; i < 25; i++) {
        uf.union(i * 4, i * 4 + 2)
      }
      
      // 验证最终状态
      const finalStats = {
        componentCount: uf.getComponentCount(),
        maxSize: uf.getMaxComponentSize(),
        analysis: uf.analyzeComponents(),
        components: uf.getAllComponents()
      }
      
      // 验证所有统计信息都合理
      return finalStats.componentCount === 75 && // 100 - 25 = 75
             finalStats.maxSize === 4 &&
             finalStats.analysis.componentCount === 75 &&
             finalStats.components.length === 75 &&
             typeof finalStats.componentCount === 'number' &&
             typeof finalStats.maxSize === 'number' &&
             typeof finalStats.analysis === 'object' &&
             Array.isArray(finalStats.components)
    })()
  )
}

testUnionFindComplexScenarios()
